#!/bin/bash
# Filename: nethome.sh

MAC="00:E0:4C:20:09:85"
IP=192.168.9.216
NETMASK=255.255.255.0
GATEWAY=192.168.9.1
DNS=202.106.0.20
DEVICE=eth0

if [ $UID != 0 ]; then
    echo "Please run this script as root!"
    exit 1
fi

# 检测网络管理软件
net_manager="network-manager"
if which wicd > /dev/null 2>&1; then
    net_manager="wicd"
fi

case $net_manager in 
    network-manager)
        echo "Stopping the $net_manager..."
        /etc/init.d/$net_manager stop &>/dev/null
        killall nm-applet > /dev/null 2>&1
        ;;
    wicd)
        echo "Stopping the $net_manager..."
        /etc/init.d/$net_manager stop &>/dev/null
        killall wicd-client > /dev/null 2>&1
        ;;
esac

ifconfig $DEVICE down
ifconfig $DEVICE $IP netmask $NETMASK hw ether $MAC
route add default gw $GATEWAY

if grep "$DNS" /etc/resolv.conf >/dev/null 2>&1; then
    :
else
    echo -e "nameserver\t$DNS" >> /etc/resolv.conf
fi
echo "Wire device $DEVICE OK!"

##########################
# 配置无线网卡模式
wless_device=eth1
wless_ip=172.16.80.100
wless_netmask=255.255.255.0
wless_essid=razone

echo "1" > /proc/sys/net/ipv4/ip_forward 
iwconfig $wless_device mode Ad-Hoc essid $wless_essid channel 1
ifconfig $wless_device $wless_ip netmask $wless_netmask
iptables -F 
iptables -P INPUT ACCEPT 
iptables -P FORWARD ACCEPT 
iptables -t nat -A POSTROUTING -o $DEVICE -j MASQUERADE
echo "Wireless $wless_device OK!"
exit 0
